﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using GPSTrackingDBV2.Entities;
using GPSTrackingDBV2.Data;

#endregion

namespace GPSTrackingDBV2.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="StockProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class StockProviderBaseCore : EntityProviderBase<GPSTrackingDBV2.Entities.Stock, GPSTrackingDBV2.Entities.StockKey>
	{		
		#region Get from Many To Many Relationship Functions
		#region GetByCompanyIdEmailToSendStockIdFromEmailToSendStockByStocks
		
		/// <summary>
		///		Gets Stock objects from the datasource by CompanyID in the
		///		EmailToSendStockByStocks table. Table Stock is related to table EmailToSendStocks
		///		through the (M:N) relationship defined in the EmailToSendStockByStocks table.
		/// </summary>
		/// <param name="_companyId"></param>
		/// <param name="_emailToSendStockId"></param>
		/// <returns>Returns a typed collection of Stock objects.</returns>
		public TList<Stock> GetByCompanyIdEmailToSendStockIdFromEmailToSendStockByStocks(System.Int32 _companyId, System.Int32 _emailToSendStockId)
		{
			int count = -1;
			return GetByCompanyIdEmailToSendStockIdFromEmailToSendStockByStocks(null,_companyId, _emailToSendStockId, 0, int.MaxValue, out count);
			
		}
		
		/// <summary>
		///		Gets GPSTrackingDBV2.Entities.Stock objects from the datasource by CompanyID in the
		///		EmailToSendStockByStocks table. Table Stock is related to table EmailToSendStocks
		///		through the (M:N) relationship defined in the EmailToSendStockByStocks table.
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_companyId"></param>
		/// <param name="_emailToSendStockId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of Stock objects.</returns>
		public TList<Stock> GetByCompanyIdEmailToSendStockIdFromEmailToSendStockByStocks(System.Int32 _companyId, System.Int32 _emailToSendStockId, int start, int pageLength)
		{
			int count = -1;
			return GetByCompanyIdEmailToSendStockIdFromEmailToSendStockByStocks(null, _companyId, _emailToSendStockId, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets Stock objects from the datasource by CompanyID in the
		///		EmailToSendStockByStocks table. Table Stock is related to table EmailToSendStocks
		///		through the (M:N) relationship defined in the EmailToSendStockByStocks table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_companyId"></param>
		/// <param name="_emailToSendStockId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Stock objects.</returns>
		public TList<Stock> GetByCompanyIdEmailToSendStockIdFromEmailToSendStockByStocks(TransactionManager transactionManager, System.Int32 _companyId, System.Int32 _emailToSendStockId)
		{
			int count = -1;
			return GetByCompanyIdEmailToSendStockIdFromEmailToSendStockByStocks(transactionManager, _companyId, _emailToSendStockId, 0, int.MaxValue, out count);
		}
		
		
		/// <summary>
		///		Gets Stock objects from the datasource by CompanyID in the
		///		EmailToSendStockByStocks table. Table Stock is related to table EmailToSendStocks
		///		through the (M:N) relationship defined in the EmailToSendStockByStocks table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_companyId"></param>
		/// <param name="_emailToSendStockId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Stock objects.</returns>
		public TList<Stock> GetByCompanyIdEmailToSendStockIdFromEmailToSendStockByStocks(TransactionManager transactionManager, System.Int32 _companyId, System.Int32 _emailToSendStockId,int start, int pageLength)
		{
			int count = -1;
			return GetByCompanyIdEmailToSendStockIdFromEmailToSendStockByStocks(transactionManager, _companyId, _emailToSendStockId, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets Stock objects from the datasource by CompanyID in the
		///		EmailToSendStockByStocks table. Table Stock is related to table EmailToSendStocks
		///		through the (M:N) relationship defined in the EmailToSendStockByStocks table.
		/// </summary>
		/// <param name="_companyId"></param>
		/// <param name="_emailToSendStockId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Stock objects.</returns>
		public TList<Stock> GetByCompanyIdEmailToSendStockIdFromEmailToSendStockByStocks(System.Int32 _companyId, System.Int32 _emailToSendStockId,int start, int pageLength, out int count)
		{
			
			return GetByCompanyIdEmailToSendStockIdFromEmailToSendStockByStocks(null, _companyId, _emailToSendStockId, start, pageLength, out count);
		}


		/// <summary>
		///		Gets Stock objects from the datasource by CompanyID in the
		///		EmailToSendStockByStocks table. Table Stock is related to table EmailToSendStocks
		///		through the (M:N) relationship defined in the EmailToSendStockByStocks table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <param name="_companyId"></param>
		/// <param name="_emailToSendStockId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of Stock objects.</returns>
		public abstract TList<Stock> GetByCompanyIdEmailToSendStockIdFromEmailToSendStockByStocks(TransactionManager transactionManager,System.Int32 _companyId, System.Int32 _emailToSendStockId, int start, int pageLength, out int count);
		
		#endregion GetByCompanyIdEmailToSendStockIdFromEmailToSendStockByStocks
		
		#region GetByCompanyIdOrderIdFromOrderDetail
		
		/// <summary>
		///		Gets Stock objects from the datasource by CompanyID in the
		///		OrderDetail table. Table Stock is related to table Order
		///		through the (M:N) relationship defined in the OrderDetail table.
		/// </summary>
		/// <param name="_companyId"></param>
		/// <param name="_orderId"></param>
		/// <returns>Returns a typed collection of Stock objects.</returns>
		public TList<Stock> GetByCompanyIdOrderIdFromOrderDetail(System.Int32 _companyId, System.Int32 _orderId)
		{
			int count = -1;
			return GetByCompanyIdOrderIdFromOrderDetail(null,_companyId, _orderId, 0, int.MaxValue, out count);
			
		}
		
		/// <summary>
		///		Gets GPSTrackingDBV2.Entities.Stock objects from the datasource by CompanyID in the
		///		OrderDetail table. Table Stock is related to table Order
		///		through the (M:N) relationship defined in the OrderDetail table.
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_companyId"></param>
		/// <param name="_orderId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of Stock objects.</returns>
		public TList<Stock> GetByCompanyIdOrderIdFromOrderDetail(System.Int32 _companyId, System.Int32 _orderId, int start, int pageLength)
		{
			int count = -1;
			return GetByCompanyIdOrderIdFromOrderDetail(null, _companyId, _orderId, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets Stock objects from the datasource by CompanyID in the
		///		OrderDetail table. Table Stock is related to table Order
		///		through the (M:N) relationship defined in the OrderDetail table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_companyId"></param>
		/// <param name="_orderId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Stock objects.</returns>
		public TList<Stock> GetByCompanyIdOrderIdFromOrderDetail(TransactionManager transactionManager, System.Int32 _companyId, System.Int32 _orderId)
		{
			int count = -1;
			return GetByCompanyIdOrderIdFromOrderDetail(transactionManager, _companyId, _orderId, 0, int.MaxValue, out count);
		}
		
		
		/// <summary>
		///		Gets Stock objects from the datasource by CompanyID in the
		///		OrderDetail table. Table Stock is related to table Order
		///		through the (M:N) relationship defined in the OrderDetail table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_companyId"></param>
		/// <param name="_orderId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Stock objects.</returns>
		public TList<Stock> GetByCompanyIdOrderIdFromOrderDetail(TransactionManager transactionManager, System.Int32 _companyId, System.Int32 _orderId,int start, int pageLength)
		{
			int count = -1;
			return GetByCompanyIdOrderIdFromOrderDetail(transactionManager, _companyId, _orderId, start, pageLength, out count);
		}
		
		/// <summary>
		///		Gets Stock objects from the datasource by CompanyID in the
		///		OrderDetail table. Table Stock is related to table Order
		///		through the (M:N) relationship defined in the OrderDetail table.
		/// </summary>
		/// <param name="_companyId"></param>
		/// <param name="_orderId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of Stock objects.</returns>
		public TList<Stock> GetByCompanyIdOrderIdFromOrderDetail(System.Int32 _companyId, System.Int32 _orderId,int start, int pageLength, out int count)
		{
			
			return GetByCompanyIdOrderIdFromOrderDetail(null, _companyId, _orderId, start, pageLength, out count);
		}


		/// <summary>
		///		Gets Stock objects from the datasource by CompanyID in the
		///		OrderDetail table. Table Stock is related to table Order
		///		through the (M:N) relationship defined in the OrderDetail table.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <param name="_companyId"></param>
		/// <param name="_orderId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a TList of Stock objects.</returns>
		public abstract TList<Stock> GetByCompanyIdOrderIdFromOrderDetail(TransactionManager transactionManager,System.Int32 _companyId, System.Int32 _orderId, int start, int pageLength, out int count);
		
		#endregion GetByCompanyIdOrderIdFromOrderDetail
		
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, GPSTrackingDBV2.Entities.StockKey key)
		{
			return Delete(transactionManager, key.CompanyId, key.StockId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="_companyId">. Primary Key.</param>
		/// <param name="_stockId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Int32 _companyId, System.Int32 _stockId)
		{
			return Delete(null, _companyId, _stockId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_companyId">. Primary Key.</param>
		/// <param name="_stockId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Int32 _companyId, System.Int32 _stockId);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Stock_Companies key.
		///		FK_Stock_Companies Description: 
		/// </summary>
		/// <param name="_companyId"></param>
		/// <returns>Returns a typed collection of GPSTrackingDBV2.Entities.Stock objects.</returns>
		public TList<Stock> GetByCompanyId(System.Int32 _companyId)
		{
			int count = -1;
			return GetByCompanyId(_companyId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Stock_Companies key.
		///		FK_Stock_Companies Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_companyId"></param>
		/// <returns>Returns a typed collection of GPSTrackingDBV2.Entities.Stock objects.</returns>
		/// <remarks></remarks>
		public TList<Stock> GetByCompanyId(TransactionManager transactionManager, System.Int32 _companyId)
		{
			int count = -1;
			return GetByCompanyId(transactionManager, _companyId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_Stock_Companies key.
		///		FK_Stock_Companies Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_companyId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of GPSTrackingDBV2.Entities.Stock objects.</returns>
		public TList<Stock> GetByCompanyId(TransactionManager transactionManager, System.Int32 _companyId, int start, int pageLength)
		{
			int count = -1;
			return GetByCompanyId(transactionManager, _companyId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Stock_Companies key.
		///		fkStockCompanies Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_companyId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of GPSTrackingDBV2.Entities.Stock objects.</returns>
		public TList<Stock> GetByCompanyId(System.Int32 _companyId, int start, int pageLength)
		{
			int count =  -1;
			return GetByCompanyId(null, _companyId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Stock_Companies key.
		///		fkStockCompanies Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_companyId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of GPSTrackingDBV2.Entities.Stock objects.</returns>
		public TList<Stock> GetByCompanyId(System.Int32 _companyId, int start, int pageLength,out int count)
		{
			return GetByCompanyId(null, _companyId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Stock_Companies key.
		///		FK_Stock_Companies Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_companyId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of GPSTrackingDBV2.Entities.Stock objects.</returns>
		public abstract TList<Stock> GetByCompanyId(TransactionManager transactionManager, System.Int32 _companyId, int start, int pageLength, out int count);
		
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Stock_StockCategoriesByCompanies key.
		///		FK_Stock_StockCategoriesByCompanies Description: 
		/// </summary>
		/// <param name="_companyId"></param>
		/// <param name="_stockCategoryId"></param>
		/// <returns>Returns a typed collection of GPSTrackingDBV2.Entities.Stock objects.</returns>
		public TList<Stock> GetByCompanyIdStockCategoryId(System.Int32 _companyId, System.Int32? _stockCategoryId)
		{
			int count = -1;
			return GetByCompanyIdStockCategoryId(_companyId, _stockCategoryId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Stock_StockCategoriesByCompanies key.
		///		FK_Stock_StockCategoriesByCompanies Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_companyId"></param>
		/// <param name="_stockCategoryId"></param>
		/// <returns>Returns a typed collection of GPSTrackingDBV2.Entities.Stock objects.</returns>
		/// <remarks></remarks>
		public TList<Stock> GetByCompanyIdStockCategoryId(TransactionManager transactionManager, System.Int32 _companyId, System.Int32? _stockCategoryId)
		{
			int count = -1;
			return GetByCompanyIdStockCategoryId(transactionManager, _companyId, _stockCategoryId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_Stock_StockCategoriesByCompanies key.
		///		FK_Stock_StockCategoriesByCompanies Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_companyId"></param>
		/// <param name="_stockCategoryId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of GPSTrackingDBV2.Entities.Stock objects.</returns>
		public TList<Stock> GetByCompanyIdStockCategoryId(TransactionManager transactionManager, System.Int32 _companyId, System.Int32? _stockCategoryId, int start, int pageLength)
		{
			int count = -1;
			return GetByCompanyIdStockCategoryId(transactionManager, _companyId, _stockCategoryId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Stock_StockCategoriesByCompanies key.
		///		fkStockStockCategoriesByCompanies Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_companyId"></param>
		/// <param name="_stockCategoryId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of GPSTrackingDBV2.Entities.Stock objects.</returns>
		public TList<Stock> GetByCompanyIdStockCategoryId(System.Int32 _companyId, System.Int32? _stockCategoryId, int start, int pageLength)
		{
			int count =  -1;
			return GetByCompanyIdStockCategoryId(null, _companyId, _stockCategoryId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Stock_StockCategoriesByCompanies key.
		///		fkStockStockCategoriesByCompanies Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="_companyId"></param>
		/// <param name="_stockCategoryId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of GPSTrackingDBV2.Entities.Stock objects.</returns>
		public TList<Stock> GetByCompanyIdStockCategoryId(System.Int32 _companyId, System.Int32? _stockCategoryId, int start, int pageLength,out int count)
		{
			return GetByCompanyIdStockCategoryId(null, _companyId, _stockCategoryId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_Stock_StockCategoriesByCompanies key.
		///		FK_Stock_StockCategoriesByCompanies Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_companyId"></param>
		/// <param name="_stockCategoryId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of GPSTrackingDBV2.Entities.Stock objects.</returns>
		public abstract TList<Stock> GetByCompanyIdStockCategoryId(TransactionManager transactionManager, System.Int32 _companyId, System.Int32? _stockCategoryId, int start, int pageLength, out int count);
		
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override GPSTrackingDBV2.Entities.Stock Get(TransactionManager transactionManager, GPSTrackingDBV2.Entities.StockKey key, int start, int pageLength)
		{
			return GetByCompanyIdStockId(transactionManager, key.CompanyId, key.StockId, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_Stock index.
		/// </summary>
		/// <param name="_companyId"></param>
		/// <param name="_stockId"></param>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.Stock"/> class.</returns>
		public GPSTrackingDBV2.Entities.Stock GetByCompanyIdStockId(System.Int32 _companyId, System.Int32 _stockId)
		{
			int count = -1;
			return GetByCompanyIdStockId(null,_companyId, _stockId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Stock index.
		/// </summary>
		/// <param name="_companyId"></param>
		/// <param name="_stockId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.Stock"/> class.</returns>
		public GPSTrackingDBV2.Entities.Stock GetByCompanyIdStockId(System.Int32 _companyId, System.Int32 _stockId, int start, int pageLength)
		{
			int count = -1;
			return GetByCompanyIdStockId(null, _companyId, _stockId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Stock index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_companyId"></param>
		/// <param name="_stockId"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.Stock"/> class.</returns>
		public GPSTrackingDBV2.Entities.Stock GetByCompanyIdStockId(TransactionManager transactionManager, System.Int32 _companyId, System.Int32 _stockId)
		{
			int count = -1;
			return GetByCompanyIdStockId(transactionManager, _companyId, _stockId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Stock index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_companyId"></param>
		/// <param name="_stockId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.Stock"/> class.</returns>
		public GPSTrackingDBV2.Entities.Stock GetByCompanyIdStockId(TransactionManager transactionManager, System.Int32 _companyId, System.Int32 _stockId, int start, int pageLength)
		{
			int count = -1;
			return GetByCompanyIdStockId(transactionManager, _companyId, _stockId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Stock index.
		/// </summary>
		/// <param name="_companyId"></param>
		/// <param name="_stockId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.Stock"/> class.</returns>
		public GPSTrackingDBV2.Entities.Stock GetByCompanyIdStockId(System.Int32 _companyId, System.Int32 _stockId, int start, int pageLength, out int count)
		{
			return GetByCompanyIdStockId(null, _companyId, _stockId, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_Stock index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="_companyId"></param>
		/// <param name="_stockId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="GPSTrackingDBV2.Entities.Stock"/> class.</returns>
		public abstract GPSTrackingDBV2.Entities.Stock GetByCompanyIdStockId(TransactionManager transactionManager, System.Int32 _companyId, System.Int32 _stockId, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a TList&lt;Stock&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="TList&lt;Stock&gt;"/></returns>
		public static TList<Stock> Fill(IDataReader reader, TList<Stock> rows, int start, int pageLength)
		{
			NetTiersProvider currentProvider = DataRepository.Provider;
            bool useEntityFactory = currentProvider.UseEntityFactory;
            bool enableEntityTracking = currentProvider.EnableEntityTracking;
            LoadPolicy currentLoadPolicy = currentProvider.CurrentLoadPolicy;
			Type entityCreationFactoryType = currentProvider.EntityCreationalFactoryType;
			
			// advance to the starting row
			for (int i = 0; i < start; i++)
			{
				if (!reader.Read())
				return rows; // not enough rows, just return
			}
			for (int i = 0; i < pageLength; i++)
			{
				if (!reader.Read())
					break; // we are done
					
				string key = null;
				
				GPSTrackingDBV2.Entities.Stock c = null;
				if (useEntityFactory)
				{
					key = new System.Text.StringBuilder("Stock")
					.Append("|").Append((System.Int32)reader[((int)StockColumn.CompanyId - 1)])
					.Append("|").Append((System.Int32)reader[((int)StockColumn.StockId - 1)]).ToString();
					c = EntityManager.LocateOrCreate<Stock>(
					key.ToString(), // EntityTrackingKey
					"Stock",  //Creational Type
					entityCreationFactoryType,  //Factory used to create entity
					enableEntityTracking); // Track this entity?
				}
				else
				{
					c = new GPSTrackingDBV2.Entities.Stock();
				}
				
				if (!enableEntityTracking ||
					c.EntityState == EntityState.Added ||
					(enableEntityTracking &&
					
						(
							(currentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
							(currentLoadPolicy == LoadPolicy.DiscardChanges && c.EntityState != EntityState.Unchanged)
						)
					))
				{
					c.SuppressEntityEvents = true;
					c.CompanyId = (System.Int32)reader[((int)StockColumn.CompanyId - 1)];
					c.OriginalCompanyId = c.CompanyId;
					c.StockId = (System.Int32)reader[((int)StockColumn.StockId - 1)];
					c.OriginalStockId = c.StockId;
					c.StockCode = (reader.IsDBNull(((int)StockColumn.StockCode - 1)))?null:(System.String)reader[((int)StockColumn.StockCode - 1)];
					c.StockBarCode = (reader.IsDBNull(((int)StockColumn.StockBarCode - 1)))?null:(System.String)reader[((int)StockColumn.StockBarCode - 1)];
					c.Name = (System.String)reader[((int)StockColumn.Name - 1)];
					c.Description = (reader.IsDBNull(((int)StockColumn.Description - 1)))?null:(System.String)reader[((int)StockColumn.Description - 1)];
					c.PromotionalNote = (reader.IsDBNull(((int)StockColumn.PromotionalNote - 1)))?null:(System.String)reader[((int)StockColumn.PromotionalNote - 1)];
					c.Brand = (reader.IsDBNull(((int)StockColumn.Brand - 1)))?null:(System.String)reader[((int)StockColumn.Brand - 1)];
					c.UnitStock = (reader.IsDBNull(((int)StockColumn.UnitStock - 1)))?null:(System.String)reader[((int)StockColumn.UnitStock - 1)];
					c.UnitStockByBox = (reader.IsDBNull(((int)StockColumn.UnitStockByBox - 1)))?null:(System.String)reader[((int)StockColumn.UnitStockByBox - 1)];
					c.StockCategoryId = (reader.IsDBNull(((int)StockColumn.StockCategoryId - 1)))?null:(System.Int32?)reader[((int)StockColumn.StockCategoryId - 1)];
					c.Quantity = (System.Int32)reader[((int)StockColumn.Quantity - 1)];
					c.MinQuantity = (reader.IsDBNull(((int)StockColumn.MinQuantity - 1)))?null:(System.Int32?)reader[((int)StockColumn.MinQuantity - 1)];
					c.Price = (System.Decimal)reader[((int)StockColumn.Price - 1)];
					c.ImageUrl = (reader.IsDBNull(((int)StockColumn.ImageUrl - 1)))?null:(System.String)reader[((int)StockColumn.ImageUrl - 1)];
					c.PdfDetailUrl = (reader.IsDBNull(((int)StockColumn.PdfDetailUrl - 1)))?null:(System.String)reader[((int)StockColumn.PdfDetailUrl - 1)];
					c.LastTick = (reader.IsDBNull(((int)StockColumn.LastTick - 1)))?null:(System.Int64?)reader[((int)StockColumn.LastTick - 1)];
					c.ReservedQuantity = (reader.IsDBNull(((int)StockColumn.ReservedQuantity - 1)))?null:(System.Int32?)reader[((int)StockColumn.ReservedQuantity - 1)];
					c.EntityTrackingKey = key;
					c.AcceptChanges();
					c.SuppressEntityEvents = false;
				}
				rows.Add(c);
			}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="GPSTrackingDBV2.Entities.Stock"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="GPSTrackingDBV2.Entities.Stock"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, GPSTrackingDBV2.Entities.Stock entity)
		{
			if (!reader.Read()) return;
			
			entity.CompanyId = (System.Int32)reader[((int)StockColumn.CompanyId - 1)];
			entity.OriginalCompanyId = (System.Int32)reader["CompanyID"];
			entity.StockId = (System.Int32)reader[((int)StockColumn.StockId - 1)];
			entity.OriginalStockId = (System.Int32)reader["StockID"];
			entity.StockCode = (reader.IsDBNull(((int)StockColumn.StockCode - 1)))?null:(System.String)reader[((int)StockColumn.StockCode - 1)];
			entity.StockBarCode = (reader.IsDBNull(((int)StockColumn.StockBarCode - 1)))?null:(System.String)reader[((int)StockColumn.StockBarCode - 1)];
			entity.Name = (System.String)reader[((int)StockColumn.Name - 1)];
			entity.Description = (reader.IsDBNull(((int)StockColumn.Description - 1)))?null:(System.String)reader[((int)StockColumn.Description - 1)];
			entity.PromotionalNote = (reader.IsDBNull(((int)StockColumn.PromotionalNote - 1)))?null:(System.String)reader[((int)StockColumn.PromotionalNote - 1)];
			entity.Brand = (reader.IsDBNull(((int)StockColumn.Brand - 1)))?null:(System.String)reader[((int)StockColumn.Brand - 1)];
			entity.UnitStock = (reader.IsDBNull(((int)StockColumn.UnitStock - 1)))?null:(System.String)reader[((int)StockColumn.UnitStock - 1)];
			entity.UnitStockByBox = (reader.IsDBNull(((int)StockColumn.UnitStockByBox - 1)))?null:(System.String)reader[((int)StockColumn.UnitStockByBox - 1)];
			entity.StockCategoryId = (reader.IsDBNull(((int)StockColumn.StockCategoryId - 1)))?null:(System.Int32?)reader[((int)StockColumn.StockCategoryId - 1)];
			entity.Quantity = (System.Int32)reader[((int)StockColumn.Quantity - 1)];
			entity.MinQuantity = (reader.IsDBNull(((int)StockColumn.MinQuantity - 1)))?null:(System.Int32?)reader[((int)StockColumn.MinQuantity - 1)];
			entity.Price = (System.Decimal)reader[((int)StockColumn.Price - 1)];
			entity.ImageUrl = (reader.IsDBNull(((int)StockColumn.ImageUrl - 1)))?null:(System.String)reader[((int)StockColumn.ImageUrl - 1)];
			entity.PdfDetailUrl = (reader.IsDBNull(((int)StockColumn.PdfDetailUrl - 1)))?null:(System.String)reader[((int)StockColumn.PdfDetailUrl - 1)];
			entity.LastTick = (reader.IsDBNull(((int)StockColumn.LastTick - 1)))?null:(System.Int64?)reader[((int)StockColumn.LastTick - 1)];
			entity.ReservedQuantity = (reader.IsDBNull(((int)StockColumn.ReservedQuantity - 1)))?null:(System.Int32?)reader[((int)StockColumn.ReservedQuantity - 1)];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="GPSTrackingDBV2.Entities.Stock"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="GPSTrackingDBV2.Entities.Stock"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, GPSTrackingDBV2.Entities.Stock entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.CompanyId = (System.Int32)dataRow["CompanyID"];
			entity.OriginalCompanyId = (System.Int32)dataRow["CompanyID"];
			entity.StockId = (System.Int32)dataRow["StockID"];
			entity.OriginalStockId = (System.Int32)dataRow["StockID"];
			entity.StockCode = Convert.IsDBNull(dataRow["StockCode"]) ? null : (System.String)dataRow["StockCode"];
			entity.StockBarCode = Convert.IsDBNull(dataRow["StockBarCode"]) ? null : (System.String)dataRow["StockBarCode"];
			entity.Name = (System.String)dataRow["Name"];
			entity.Description = Convert.IsDBNull(dataRow["Description"]) ? null : (System.String)dataRow["Description"];
			entity.PromotionalNote = Convert.IsDBNull(dataRow["PromotionalNote"]) ? null : (System.String)dataRow["PromotionalNote"];
			entity.Brand = Convert.IsDBNull(dataRow["Brand"]) ? null : (System.String)dataRow["Brand"];
			entity.UnitStock = Convert.IsDBNull(dataRow["UnitStock"]) ? null : (System.String)dataRow["UnitStock"];
			entity.UnitStockByBox = Convert.IsDBNull(dataRow["UnitStockByBox"]) ? null : (System.String)dataRow["UnitStockByBox"];
			entity.StockCategoryId = Convert.IsDBNull(dataRow["StockCategoryID"]) ? null : (System.Int32?)dataRow["StockCategoryID"];
			entity.Quantity = (System.Int32)dataRow["Quantity"];
			entity.MinQuantity = Convert.IsDBNull(dataRow["MinQuantity"]) ? null : (System.Int32?)dataRow["MinQuantity"];
			entity.Price = (System.Decimal)dataRow["Price"];
			entity.ImageUrl = Convert.IsDBNull(dataRow["ImageUrl"]) ? null : (System.String)dataRow["ImageUrl"];
			entity.PdfDetailUrl = Convert.IsDBNull(dataRow["PDFDetailUrl"]) ? null : (System.String)dataRow["PDFDetailUrl"];
			entity.LastTick = Convert.IsDBNull(dataRow["LastTick"]) ? null : (System.Int64?)dataRow["LastTick"];
			entity.ReservedQuantity = Convert.IsDBNull(dataRow["ReservedQuantity"]) ? null : (System.Int32?)dataRow["ReservedQuantity"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="GPSTrackingDBV2.Entities.Stock"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">GPSTrackingDBV2.Entities.Stock Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		public override void DeepLoad(TransactionManager transactionManager, GPSTrackingDBV2.Entities.Stock entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;

			#region CompanyIdSource	
			if (CanDeepLoad(entity, "Companies|CompanyIdSource", deepLoadType, innerList) 
				&& entity.CompanyIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = entity.CompanyId;
				Companies tmpEntity = EntityManager.LocateEntity<Companies>(EntityLocator.ConstructKeyFromPkItems(typeof(Companies), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.CompanyIdSource = tmpEntity;
				else
					entity.CompanyIdSource = DataRepository.CompaniesProvider.GetByCompanyId(transactionManager, entity.CompanyId);		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'CompanyIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.CompanyIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.CompaniesProvider.DeepLoad(transactionManager, entity.CompanyIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion CompanyIdSource

			#region CompanyIdStockCategoryIdSource	
			if (CanDeepLoad(entity, "StockCategoriesByCompanies|CompanyIdStockCategoryIdSource", deepLoadType, innerList) 
				&& entity.CompanyIdStockCategoryIdSource == null)
			{
				object[] pkItems = new object[2];
				pkItems[0] = entity.CompanyId;
				pkItems[1] = (entity.StockCategoryId ?? (int)0);
				StockCategoriesByCompanies tmpEntity = EntityManager.LocateEntity<StockCategoriesByCompanies>(EntityLocator.ConstructKeyFromPkItems(typeof(StockCategoriesByCompanies), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.CompanyIdStockCategoryIdSource = tmpEntity;
				else
					entity.CompanyIdStockCategoryIdSource = DataRepository.StockCategoriesByCompaniesProvider.GetByCompanyIdStockCategoryId(transactionManager, entity.CompanyId, (entity.StockCategoryId ?? (int)0));		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'CompanyIdStockCategoryIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.CompanyIdStockCategoryIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.StockCategoriesByCompaniesProvider.DeepLoad(transactionManager, entity.CompanyIdStockCategoryIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion CompanyIdStockCategoryIdSource
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			// Deep load child collections  - Call GetByCompanyIdStockId methods when available
			
			#region CompanyIdOrderIdOrderCollection_From_OrderDetail
			// RelationshipType.ManyToMany
			if (CanDeepLoad(entity, "List<Order>|CompanyIdOrderIdOrderCollection_From_OrderDetail", deepLoadType, innerList))
			{
				entity.CompanyIdOrderIdOrderCollection_From_OrderDetail = DataRepository.OrderProvider.GetByCompanyIdStockIdFromOrderDetail(transactionManager, entity.CompanyId, entity.StockId);			 
		
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'CompanyIdOrderIdOrderCollection_From_OrderDetail' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.CompanyIdOrderIdOrderCollection_From_OrderDetail != null)
				{
					deepHandles.Add("CompanyIdOrderIdOrderCollection_From_OrderDetail",
						new KeyValuePair<Delegate, object>((DeepLoadHandle< Order >) DataRepository.OrderProvider.DeepLoad,
						new object[] { transactionManager, entity.CompanyIdOrderIdOrderCollection_From_OrderDetail, deep, deepLoadType, childTypes, innerList }
					));
				}
			}
			#endregion
			
			
			
			#region CompanyIdEmailToSendStockIdEmailToSendStocksCollection_From_EmailToSendStockByStocks
			// RelationshipType.ManyToMany
			if (CanDeepLoad(entity, "List<EmailToSendStocks>|CompanyIdEmailToSendStockIdEmailToSendStocksCollection_From_EmailToSendStockByStocks", deepLoadType, innerList))
			{
				entity.CompanyIdEmailToSendStockIdEmailToSendStocksCollection_From_EmailToSendStockByStocks = DataRepository.EmailToSendStocksProvider.GetByCompanyIdStockIdFromEmailToSendStockByStocks(transactionManager, entity.CompanyId, entity.StockId);			 
		
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'CompanyIdEmailToSendStockIdEmailToSendStocksCollection_From_EmailToSendStockByStocks' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.CompanyIdEmailToSendStockIdEmailToSendStocksCollection_From_EmailToSendStockByStocks != null)
				{
					deepHandles.Add("CompanyIdEmailToSendStockIdEmailToSendStocksCollection_From_EmailToSendStockByStocks",
						new KeyValuePair<Delegate, object>((DeepLoadHandle< EmailToSendStocks >) DataRepository.EmailToSendStocksProvider.DeepLoad,
						new object[] { transactionManager, entity.CompanyIdEmailToSendStockIdEmailToSendStocksCollection_From_EmailToSendStockByStocks, deep, deepLoadType, childTypes, innerList }
					));
				}
			}
			#endregion
			
			
			
			#region EmailToSendStockByStocksCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<EmailToSendStockByStocks>|EmailToSendStockByStocksCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'EmailToSendStockByStocksCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.EmailToSendStockByStocksCollection = DataRepository.EmailToSendStockByStocksProvider.GetByCompanyIdStockId(transactionManager, entity.CompanyId, entity.StockId);

				if (deep && entity.EmailToSendStockByStocksCollection.Count > 0)
				{
					deepHandles.Add("EmailToSendStockByStocksCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<EmailToSendStockByStocks>) DataRepository.EmailToSendStockByStocksProvider.DeepLoad,
						new object[] { transactionManager, entity.EmailToSendStockByStocksCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			#region StockByAditionalImagesCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<StockByAditionalImages>|StockByAditionalImagesCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'StockByAditionalImagesCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.StockByAditionalImagesCollection = DataRepository.StockByAditionalImagesProvider.GetByCompanyIdStockId(transactionManager, entity.CompanyId, entity.StockId);

				if (deep && entity.StockByAditionalImagesCollection.Count > 0)
				{
					deepHandles.Add("StockByAditionalImagesCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<StockByAditionalImages>) DataRepository.StockByAditionalImagesProvider.DeepLoad,
						new object[] { transactionManager, entity.StockByAditionalImagesCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			#region OrderDetailCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<OrderDetail>|OrderDetailCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'OrderDetailCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.OrderDetailCollection = DataRepository.OrderDetailProvider.GetByCompanyIdStockId(transactionManager, entity.CompanyId, entity.StockId);

				if (deep && entity.OrderDetailCollection.Count > 0)
				{
					deepHandles.Add("OrderDetailCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<OrderDetail>) DataRepository.OrderDetailProvider.DeepLoad,
						new object[] { transactionManager, entity.OrderDetailCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the GPSTrackingDBV2.Entities.Stock object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">GPSTrackingDBV2.Entities.Stock instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">GPSTrackingDBV2.Entities.Stock Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		public override bool DeepSave(TransactionManager transactionManager, GPSTrackingDBV2.Entities.Stock entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			
			#region CompanyIdSource
			if (CanDeepSave(entity, "Companies|CompanyIdSource", deepSaveType, innerList) 
				&& entity.CompanyIdSource != null)
			{
				DataRepository.CompaniesProvider.Save(transactionManager, entity.CompanyIdSource);
				entity.CompanyId = entity.CompanyIdSource.CompanyId;
			}
			#endregion 
			
			#region CompanyIdStockCategoryIdSource
			if (CanDeepSave(entity, "StockCategoriesByCompanies|CompanyIdStockCategoryIdSource", deepSaveType, innerList) 
				&& entity.CompanyIdStockCategoryIdSource != null)
			{
				DataRepository.StockCategoriesByCompaniesProvider.Save(transactionManager, entity.CompanyIdStockCategoryIdSource);
				entity.CompanyId = entity.CompanyIdStockCategoryIdSource.CompanyId;
				entity.StockCategoryId = entity.CompanyIdStockCategoryIdSource.StockCategoryId;
			}
			#endregion 
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();

			#region CompanyIdOrderIdOrderCollection_From_OrderDetail>
			if (CanDeepSave(entity.CompanyIdOrderIdOrderCollection_From_OrderDetail, "List<Order>|CompanyIdOrderIdOrderCollection_From_OrderDetail", deepSaveType, innerList))
			{
				if (entity.CompanyIdOrderIdOrderCollection_From_OrderDetail.Count > 0 || entity.CompanyIdOrderIdOrderCollection_From_OrderDetail.DeletedItems.Count > 0)
				{
					DataRepository.OrderProvider.Save(transactionManager, entity.CompanyIdOrderIdOrderCollection_From_OrderDetail); 
					deepHandles.Add("CompanyIdOrderIdOrderCollection_From_OrderDetail",
						new KeyValuePair<Delegate, object>((DeepSaveHandle<Order>) DataRepository.OrderProvider.DeepSave,
						new object[] { transactionManager, entity.CompanyIdOrderIdOrderCollection_From_OrderDetail, deepSaveType, childTypes, innerList }
					));
				}
			}
			#endregion 

			#region CompanyIdEmailToSendStockIdEmailToSendStocksCollection_From_EmailToSendStockByStocks>
			if (CanDeepSave(entity.CompanyIdEmailToSendStockIdEmailToSendStocksCollection_From_EmailToSendStockByStocks, "List<EmailToSendStocks>|CompanyIdEmailToSendStockIdEmailToSendStocksCollection_From_EmailToSendStockByStocks", deepSaveType, innerList))
			{
				if (entity.CompanyIdEmailToSendStockIdEmailToSendStocksCollection_From_EmailToSendStockByStocks.Count > 0 || entity.CompanyIdEmailToSendStockIdEmailToSendStocksCollection_From_EmailToSendStockByStocks.DeletedItems.Count > 0)
				{
					DataRepository.EmailToSendStocksProvider.Save(transactionManager, entity.CompanyIdEmailToSendStockIdEmailToSendStocksCollection_From_EmailToSendStockByStocks); 
					deepHandles.Add("CompanyIdEmailToSendStockIdEmailToSendStocksCollection_From_EmailToSendStockByStocks",
						new KeyValuePair<Delegate, object>((DeepSaveHandle<EmailToSendStocks>) DataRepository.EmailToSendStocksProvider.DeepSave,
						new object[] { transactionManager, entity.CompanyIdEmailToSendStockIdEmailToSendStocksCollection_From_EmailToSendStockByStocks, deepSaveType, childTypes, innerList }
					));
				}
			}
			#endregion 
	
			#region List<EmailToSendStockByStocks>
				if (CanDeepSave(entity.EmailToSendStockByStocksCollection, "List<EmailToSendStockByStocks>|EmailToSendStockByStocksCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(EmailToSendStockByStocks child in entity.EmailToSendStockByStocksCollection)
					{
						if(child.CompanyIdStockIdSource != null)
						{
								child.CompanyId = child.CompanyIdStockIdSource.CompanyId;
								child.StockId = child.CompanyIdStockIdSource.StockId;
						}

						if(child.CompanyIdEmailToSendStockIdSource != null)
						{
								child.CompanyId = child.CompanyIdEmailToSendStockIdSource.CompanyId;
								child.EmailToSendStockId = child.CompanyIdEmailToSendStockIdSource.EmailToSendStockId;
						}

					}

					if (entity.EmailToSendStockByStocksCollection.Count > 0 || entity.EmailToSendStockByStocksCollection.DeletedItems.Count > 0)
					{
						//DataRepository.EmailToSendStockByStocksProvider.Save(transactionManager, entity.EmailToSendStockByStocksCollection);
						
						deepHandles.Add("EmailToSendStockByStocksCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< EmailToSendStockByStocks >) DataRepository.EmailToSendStockByStocksProvider.DeepSave,
							new object[] { transactionManager, entity.EmailToSendStockByStocksCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
	
			#region List<StockByAditionalImages>
				if (CanDeepSave(entity.StockByAditionalImagesCollection, "List<StockByAditionalImages>|StockByAditionalImagesCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(StockByAditionalImages child in entity.StockByAditionalImagesCollection)
					{
						if(child.CompanyIdStockIdSource != null)
						{
							child.CompanyId = child.CompanyIdStockIdSource.CompanyId;
							child.StockId = child.CompanyIdStockIdSource.StockId;
						}
						else
						{
							child.CompanyId = entity.CompanyId;
							child.StockId = entity.StockId;
						}

					}

					if (entity.StockByAditionalImagesCollection.Count > 0 || entity.StockByAditionalImagesCollection.DeletedItems.Count > 0)
					{
						//DataRepository.StockByAditionalImagesProvider.Save(transactionManager, entity.StockByAditionalImagesCollection);
						
						deepHandles.Add("StockByAditionalImagesCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< StockByAditionalImages >) DataRepository.StockByAditionalImagesProvider.DeepSave,
							new object[] { transactionManager, entity.StockByAditionalImagesCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
	
			#region List<OrderDetail>
				if (CanDeepSave(entity.OrderDetailCollection, "List<OrderDetail>|OrderDetailCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(OrderDetail child in entity.OrderDetailCollection)
					{
						if(child.CompanyIdStockIdSource != null)
						{
								child.CompanyId = child.CompanyIdStockIdSource.CompanyId;
								child.StockId = child.CompanyIdStockIdSource.StockId;
						}

						if(child.CompanyIdOrderIdSource != null)
						{
								child.CompanyId = child.CompanyIdOrderIdSource.CompanyId;
								child.OrderId = child.CompanyIdOrderIdSource.OrderId;
						}

					}

					if (entity.OrderDetailCollection.Count > 0 || entity.OrderDetailCollection.DeletedItems.Count > 0)
					{
						//DataRepository.OrderDetailProvider.Save(transactionManager, entity.OrderDetailCollection);
						
						deepHandles.Add("OrderDetailCollection",
						new KeyValuePair<Delegate, object>((DeepSaveHandle< OrderDetail >) DataRepository.OrderDetailProvider.DeepSave,
							new object[] { transactionManager, entity.OrderDetailCollection, deepSaveType, childTypes, innerList }
						));
					}
				} 
			#endregion 
				
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region StockChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>GPSTrackingDBV2.Entities.Stock</c>
	///</summary>
	public enum StockChildEntityTypes
	{
		
		///<summary>
		/// Composite Property for <c>Companies</c> at CompanyIdSource
		///</summary>
		[ChildEntityType(typeof(Companies))]
		Companies,
			
		///<summary>
		/// Composite Property for <c>StockCategoriesByCompanies</c> at CompanyIdStockCategoryIdSource
		///</summary>
		[ChildEntityType(typeof(StockCategoriesByCompanies))]
		StockCategoriesByCompanies,
	
		///<summary>
		/// Collection of <c>Stock</c> as ManyToMany for OrderCollection_From_OrderDetail
		///</summary>
		[ChildEntityType(typeof(TList<Order>))]
		CompanyIdOrderIdOrderCollection_From_OrderDetail,

		///<summary>
		/// Collection of <c>Stock</c> as ManyToMany for EmailToSendStocksCollection_From_EmailToSendStockByStocks
		///</summary>
		[ChildEntityType(typeof(TList<EmailToSendStocks>))]
		CompanyIdEmailToSendStockIdEmailToSendStocksCollection_From_EmailToSendStockByStocks,

		///<summary>
		/// Collection of <c>Stock</c> as OneToMany for EmailToSendStockByStocksCollection
		///</summary>
		[ChildEntityType(typeof(TList<EmailToSendStockByStocks>))]
		EmailToSendStockByStocksCollection,

		///<summary>
		/// Collection of <c>Stock</c> as OneToMany for StockByAditionalImagesCollection
		///</summary>
		[ChildEntityType(typeof(TList<StockByAditionalImages>))]
		StockByAditionalImagesCollection,

		///<summary>
		/// Collection of <c>Stock</c> as OneToMany for OrderDetailCollection
		///</summary>
		[ChildEntityType(typeof(TList<OrderDetail>))]
		OrderDetailCollection,
	}
	
	#endregion StockChildEntityTypes
	
	#region StockFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;StockColumn&gt;"/> class
	/// that is used exclusively with a <see cref="Stock"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class StockFilterBuilder : SqlFilterBuilder<StockColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the StockFilterBuilder class.
		/// </summary>
		public StockFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the StockFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public StockFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the StockFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public StockFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion StockFilterBuilder
	
	#region StockParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;StockColumn&gt;"/> class
	/// that is used exclusively with a <see cref="Stock"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class StockParameterBuilder : ParameterizedSqlFilterBuilder<StockColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the StockParameterBuilder class.
		/// </summary>
		public StockParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the StockParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public StockParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the StockParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public StockParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion StockParameterBuilder
	
	#region StockSortBuilder
    
    /// <summary>
    /// A strongly-typed instance of the <see cref="SqlSortBuilder&lt;StockColumn&gt;"/> class
	/// that is used exclusively with a <see cref="Stock"/> object.
    /// </summary>
    [CLSCompliant(true)]
    public class StockSortBuilder : SqlSortBuilder<StockColumn>
    {
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the StockSqlSortBuilder class.
		/// </summary>
		public StockSortBuilder() : base() { }

		#endregion Constructors

    }    
    #endregion StockSortBuilder
	
} // end namespace
